﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_website_news_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }
            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlCommunity.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlCommunity.DataBind();

            this.ddlNewsClass.DataSource = dal.GetWebSiteNewsClass(int.Parse(this.ddlCommunity.SelectedValue), null);
            this.ddlNewsClass.DataBind();
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.DelWebSiteNews(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {
                
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.GetWebSiteNews(int.Parse(hidId.Value));
                if (dt.Rows.Count > 0)
                {
                    this.ddlCommunity.Items.FindByValue(dt.Rows[0]["self_community_code"].ToString()).Selected = true;
                    this.ddlNewsClass.Items.FindByValue(dt.Rows[0]["classid"].ToString()).Selected = true;
                    this.txtTitle.Text = dt.Rows[0]["title"].ToString();
                    this.editor_id.Value = dt.Rows[0]["content"].ToString();
                    if (Convert.ToString(dt.Rows[0]["titlepic"]) != "") this.lblpic.Text = "<font color=red>图片已添加</font>"; this.hidpic.Value = Convert.ToString(dt.Rows[0]["titlepic"]);
                }
            }
        }

        protected void cmdOk_Click(object sender, EventArgs e)
        {
            string sCommunity = this.ddlCommunity.SelectedValue;
            string sClass = this.ddlNewsClass.SelectedValue;
            string sTitle = this.txtTitle.Text.Trim();
            string sTitlepic = this.hidpic.Value;
            string sContent = this.editor_id.Value.Trim();
            
            if (sTitle == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "标题不能为空！");
                return;
            }

            if (this.filePic.HasFile) {
                string fullname = this.filePic.PostedFile.FileName;
                string ext = fullname.Substring(fullname.LastIndexOf(".") + 1).ToLower();
                if (ext == "jpg" || ext == "gif" || ext == "bmp" || ext == "png")
                {
                    string sSaveFolder = Server.MapPath("~/uploadpic");
                    string sNewFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + PM.Web.Common.GetRandomNumber(5) + "." + ext;
                    try
                    {
                        if (!System.IO.Directory.Exists(sSaveFolder)) System.IO.Directory.CreateDirectory(sSaveFolder);
                        this.filePic.SaveAs(sSaveFolder + "/" + sNewFileName);
                        sTitlepic = "uploadpic/" + sNewFileName;
                    }
                    catch
                    {
                        PM.Web.Common.Alert(this, "上传失败！");
                    }
                }
                else
                {
                   PM.Web.Common.Alert(this, "只能上传图片！");
                }
            }
            
            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.AddWebSiteNews(int.Parse(sCommunity),int.Parse(this.ddlNewsClass.SelectedValue), sTitle, sTitlepic, sContent))
                {
                    PM.Web.Common.JsAlert(this, "保存成功！", true);
                }
            }
            else
            {
                if (dal.ModifyWebSiteNews(int.Parse(hidId.Value), int.Parse(sCommunity),int.Parse(this.ddlNewsClass.SelectedValue), sTitle, sTitlepic,sContent))
                {
                    PM.Web.Common.JsAlert(this, "修改成功！", true);
                }
            }
        }
    }

}